#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk, os
import utils

class list_photo(gtk.TreeView):
    def __init__(self, project_folder):
        gtk.TreeView.__init__(self)
        self.project_folder=project_folder
        
        self.photos=[]
        self.read_project_file()
        self.model = gtk.ListStore (gtk.gdk.Pixbuf, str, str)
        COL_ICON, COL_NAME, COL_PATH = (0, 1, 2)
        
        self.set_model(self.model)
        self.treeselection = self.get_selection()
        self.treeselection.set_mode (gtk.SELECTION_SINGLE)

        cell = gtk.CellRendererPixbuf ()
        column = gtk.TreeViewColumn ('#', cell, pixbuf = COL_ICON)
        self.append_column (column)
    
        cell = gtk.CellRendererText ()
        column = gtk.TreeViewColumn ('Name', cell, text = COL_NAME)
        self.append_column (column)
        
        self.load()
    
    def read_project_file(self):
        try:
            file=open(self.project_folder+"project.GMM", "r")
            for el in file.readlines():
                el=el.replace("\n", "")
                print el
                if os.path.exists(el):
                    self.photos.append(el)
        except: pass
    
    def load(self):
        for photo in self.photos:
            self.model.append([utils.getPixbufFromName(photo, 128), self.get_name(photo), photo])
    
    def get_selected(self):
        model, iter = self.treeselection.get_selected()
        if iter:
           return model.get_value (iter, 2)
        return None
        
    def refresh(self):
        self.clean_list()
        self.load()
        self.rewrite_config()
    
    def rewrite_config(self):
        file=open(self.project_folder+"project.GMM", "w")
        for el in self.get_list():
            file.write(el+"\n")
        file.close()
    
    def get_name(self, path):
        name=path.split("/")
        name=name[len(name)-1]
        return name
    
    def clean_list(self):
        self.model.clear()
        pass
        
    def delete_all(self):
    	self.clean_list()
    	self.photos=[]
    	self.rewrite_config()
    	self.load()

    def moveup(self):
        model, iter = self.treeselection.get_selected()
        if iter:
           first=None
           temp = self.model.get_iter_first ()
           last=None
           while temp:
                
                if (self.model.get_value(iter,1)==self.model.get_value (temp, 1)):
                    first=last
                    break;
                last=temp
                temp=self.model.iter_next(temp)
           
           if first!=None:
               model.swap(iter, first)
           self.rewrite_config()
        
    
    def movedown(self):
        model, iter = self.treeselection.get_selected()
        if iter:
           first=model.iter_next(iter)
           if first!=None:
               model.swap(iter, first)
           self.rewrite_config()
    
    def add(self, photo):
        self.photos.append(photo)
        self.refresh()
        pass

    def remove(self):
        (photo)=self.get_selected()
        if photo!=None:
            self.photos.remove(photo)
            self.refresh()

    def get_list(self):
        lista = []
        iter = self.model.get_iter_first ()
        while iter:
                lista.append(self.model.get_value (iter, 2))
                iter=self.model.iter_next(iter)

        return lista
